package mwt.xml.xdbforms.schemalayer.types;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import javax.xml.XMLConstants;
import mwt.xml.xdbforms.dblayer.metadata.impl.ColumnMetaData;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;

/**
 * @author Gianfranco Murador, Matteo Ferri, Cristian Castiglia
 */
public class XmlDecimal extends XmlTypes {

    public XmlDecimal() {
    }

    public XmlDecimal(Document doc) {
        super(doc);
    }

    private void buildFragment(ColumnMetaData cmd) {

        Element xtype, restriction, totalDigits, fractionDigits;
        typeName += "_" + cmd.getDisplaySize() + "_" + cmd.getFractional();
        fragment = doc.createDocumentFragment();

        xtype = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs:simpleType");
        xtype.setAttribute("name", typeName);
        cmd.setXschemaTypeName(typeName);

        restriction = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs:restriction");
        restriction.setAttribute("base", "xs:decimal");

        totalDigits = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs:totalDigits");
        totalDigits.setAttribute("value", cmd.getDisplaySize().toString());
        fractionDigits = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs:fractionDigits");
        fractionDigits.setAttribute("value", cmd.getFractional().toString());

        restriction.appendChild(totalDigits);
        restriction.appendChild(fractionDigits);

        xtype.appendChild(restriction);
        fragment.appendChild(xtype);

    }

    @Override
    public DocumentFragment getSchemaType(ColumnMetaData cmd) {

        typeName = cmd.getTypeName();

        if (!typeName.equals("DECIMAL")) {

            if (typeName.equals("FLOAT")) {
                return new XmlFloat(doc).getSchemaType(cmd);
            } else {

                if (typeName.equals("DOUBLE")) {
                    return new XmlDouble(doc).getSchemaType(cmd);
                } else {
                    return nextType.getSchemaType(cmd);
                }
            }

        } else {
            buildFragment(cmd);
            return fragment;

        }

    }

    @Override
    public Object getXSchemaToJavaType(String baseType, String value) {

        BigDecimal bigDecimal;

        if (!baseType.startsWith("DECIMAL")) {

            if (baseType.startsWith("FLOAT")) {
                return new XmlFloat().getXSchemaToJavaType(baseType, value);
            } else {

                if (baseType.startsWith("DOUBLE")) {
                    return new XmlDouble().getXSchemaToJavaType(baseType, value);
                } else {
                    return nextType.getXSchemaToJavaType(baseType, value);
                }
            }

        } else {


            System.out.println("Valore Decimale ::-> " + value);


            if (value.contains(".")) {
                bigDecimal = new BigDecimal(value);
            } else {
                bigDecimal = new BigDecimal(value + ".00");
            }

            return bigDecimal;

        }



    }
}

